package net.src.inetAddres;

import java.net.InetAddress;

public class InetAddressDemo {
    public static void main(String[] args) throws Exception {
        //获取本机的地址对象,并分别获取主机名和IP地址
        InetAddress ip1 = InetAddress.getLocalHost();
        System.out.println(ip1.getHostName());
        System.out.println(ip1.getHostAddress());

        //根据域名获取ip对象
        InetAddress ip2 = InetAddress.getByName("www.baidu.com");
        System.out.println(ip2.getHostName());
        System.out.println(ip2.getHostAddress());

        //获取公网ip对象
        InetAddress ip3 = InetAddress.getByName("192.168.43.200");
        System.out.println(ip3.getHostName());
        System.out.println(ip3.getHostAddress());

        //判断是否能联通,类似于ping,限制5s内联通
        System.out.println(ip2.isReachable(5000));
    }
}
